<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        const obj = {
            name: '东东',
            age: 28,
            hobby: ['dance', 'sing', 'rap'],
            family: {
                name: '练练',
                age: 28,
                son: {
                    name: 'dd',
                    age: 10
                }
            },
            data: [{
                salary: 2000,
                price: 1000
            }]
        }
        const o = {}
        const deepClone = (newObj, oldObj) => {
            for (let k in oldObj) {
                // 判断属性值oldObj[k]是不是数组，如果是数组，拷贝这一层的时候 新创建一个空数组，给这个空数组继续添加元素
                if (oldObj[k] instanceof Array) {
                    newObj[k] = []
                    // 给这个新的对象添加元素
                    // deepClone([], ['dance', 'sing', 'rap'])
                    // newObj[k] ==> 空数组
                    // oldObj[k] ==> 传递的是['dance', 'sing', 'rap']
                    deepClone(newObj[k], oldObj[k])
                } else if (oldObj[k] instanceof Object) {
                    newObj[k] = {}
                    deepClone(newObj[k], oldObj[k])
                } else {
                    newObj[k] = oldObj[k]
                }
            }
        }
        deepClone(o, obj)
        console.log(o)
        o.hobby[0] = 'bskt'
        o.family.age = 30
        o.family.son.age = 20
        o.data[0].salary = 3000
        console.log('---------------------------')
        console.log(obj)
    </script>
</body>

</html>